package com.company.process.ds;

import com.company.TrianglePacket;
import com.company.graph.Graph;
import com.company.graph.NopowerNoDirectEdgeWithNodeGraph;
import com.company.graph.PowerEdge;
import com.company.graphgetter.EdgeGetterWithGraph;
import com.company.graphgetter.GraphGetter;

import java.util.*;

public class GraphDS3Process extends GraphDSProcess {

    public GraphDS3Process(int theta) {
        super(theta);
    }

    @Override
    public void process(Graph graph, GraphGetter getter) {
        G = (NopowerNoDirectEdgeWithNodeGraph) graph;
        this.getter = (EdgeGetterWithGraph) getter;
        //Group all triangular edges with their ET values into "e_plus" and "e_minus" respectively
        List<TrianglePacket> edgeRanks = copyBase(this.getter.edgeRanks());
        for (TrianglePacket rank : edgeRanks) {
            setET(rank.getId(), rank.getValue());
            if (rank.getValue() >= theta) {
                e_plus.add(G.getEdgeById(rank.getId()));
            } else {
                e_minus.add(G.getEdgeById(rank.getId()));
            }
        }
        boolean oneMoreTimeFlag = true;
        while (oneMoreTimeFlag) {
            oneMoreTimeFlag = false;
            Iterator<TrianglePacket> iterator = edgeRanks.iterator();
            while (iterator.hasNext()) {
                TrianglePacket rank = iterator.next();
                int alphaId = rank.getId();
                Integer ret = getRET(alphaId);
                if (ret > theta) {
                    if (G.getEdgeById(alphaId) == null) {
                        continue;
                    }
                    List<PowerEdge> relatedEdges = G.getRelatedEdges(alphaId);
                    ArrayList<Bag> bags = Bag.generateFromEdges(relatedEdges, ET);
//                    Collections.sort(bags);
                    //需要被删除的次数
                    int deleteCount = ret - theta;
                    deleteRelatedEdgesWithBags(bags, deleteCount);
                }
            }
        }

    }

    private List<TrianglePacket> copyBase(List<TrianglePacket> edgeRanks) {
        List<TrianglePacket> buffer = new LinkedList<>();
        for (TrianglePacket edge : edgeRanks) {
            buffer.add(edge);
        }
        return buffer;
    }


}
